package com.dferreira.languagesteach.data_parsers;

import com.dferreira.languagesteach.data_models.MetaLesson;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;

public class XMLLanguageParser extends DefaultHandler {
    private final ArrayList<MetaLesson> metaLessons;

    /**
     * Starts the variables to the language parsing
     */
    public XMLLanguageParser() {
        super();
        metaLessons = new ArrayList<MetaLesson>();
    }

    /**
     * Called when tag starts
     *
     * @param uri        The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
     * @param localName  The local name (without prefix), or the empty string if Namespace processing is not being performed.
     * @param qName      The qualified name (with prefix), or the empty string if qualified names are not available.
     * @param attributes The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
     */
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) {
        String LESSON_TAG = "lesson";
        String TITLE_ATTRIBUTE = "title";
        String FOLDER_ATTRIBUTE = "folder";

        if (localName.equals(LESSON_TAG)) {
            MetaLesson metaLesson;
            metaLesson = new MetaLesson();
            metaLesson.setTitle(attributes.getValue(TITLE_ATTRIBUTE));
            metaLesson.setFolder(attributes.getValue(FOLDER_ATTRIBUTE));
            metaLessons.add(metaLesson);
        }
    }

    /**
     * @return An array of lessons of the language
     */
    public ArrayList<MetaLesson> getMetaLessons() {
        return metaLessons;
    }
}